import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account {

    private String cardID;
    private double money;
    // 创建一个Lock锁实例对象
    /* TODO */

    public Account() {
    }

    public Account(String cardID, double money) {
        this.cardID = cardID;
        this.money = money;
    }

    // 使用同步方法的方式解决取钱业务安全问题
    public synchronized void drawMoney(double money) {
        //  获取当前取钱人的名称
        String name = Thread.currentThread().getName();

        // 开始加锁
        /* TODO */
        try {
            // 判断当前账户余额是否足够
            if (this.money >= money) {
                // 余额充足，开始取钱
                System.out.println(name + "来取钱" + money + "成功！");
                this.money -= money;
                System.out.println(name + "来取钱后，余额剩余：" + this.money);
            } else {
                // 余额不足
                System.out.println(name + "来取钱：余额不足~");
            }
        } finally {
            // 开始解锁
            /* TODO */
        }
    }

    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}
